#include "sur_mount.h"
#include "fuse_impls.h"

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
/*
static struct fuse_operations sdfs_opt = {
	destroy  : sdfs_destroy,
	init     : sdfs_init,
	create   : sdfs_create,
	open     : sdfs_open,	//and getattr
	read     : sdfs_read,
	write    : sdfs_write,  //and getxattr
	flush    : sdfs_flush,
	release  : sdfs_release,
	rename   : sdfs_rename,
	unlink   : sdfs_unlink,
	getattr  : sdfs_getattr,
	readdir  : sdfs_readdir,
	truncate : sdfs_truncate,
	access   : sdfs_access,
	statfs   : sdfs_statfs,
	mkdir    : sdfs_mkdir,
	rmdir    : sdfs_rmdir,
	utimens  : sdfs_utimens,
	chmod    : sdfs_chmod,
};

*/
string config_file;
string log_file;
static struct fuse_operations sdfs_opt;
int main(int argc, char *argv[])
{
//	struct fuse_operations sdfs_opt;
	sdfs_opt.destroy  = sdfs_destroy;
	sdfs_opt.init     = sdfs_init;
	sdfs_opt.create   = sdfs_create;
	sdfs_opt.open     = sdfs_open;	//and getattr
	sdfs_opt.read     = sdfs_read;
	sdfs_opt.write    = sdfs_write;  //and getxattr
	sdfs_opt.flush    = sdfs_flush;
	sdfs_opt.release  = sdfs_release;
	sdfs_opt.rename   = sdfs_rename;
	sdfs_opt.unlink   = sdfs_unlink;
	sdfs_opt.getattr  = sdfs_getattr;
	sdfs_opt.readdir  = sdfs_readdir;
	sdfs_opt.truncate = sdfs_truncate;
	sdfs_opt.access   = sdfs_access;
	sdfs_opt.statfs   = sdfs_statfs;
	sdfs_opt.mkdir    = sdfs_mkdir;
	sdfs_opt.rmdir    = sdfs_rmdir;
	sdfs_opt.utimens  = sdfs_utimens;
	sdfs_opt.chmod    = sdfs_chmod;
	
	int ret;
	umask(0);
	
	int i = 0;
	int j = 0;
	char** nargv = (char**)malloc(argc * sizeof(char*));
	int nagrc = argc;
	for(i = 0, j = 0; i < argc; ++i)
	{
		char* eq = strstr(argv[i], "config=");
		if(eq != NULL) {
			config_file = string(eq + 7);
			--nagrc;
		} else {
			nargv[j] = argv[i];
			++j;
		}
	}	
	configure* cfg = configure::get_instance();
	cfg->get_config(config_file.c_str());
	
	logger* log = logger::get_instance();
	log->set_log_file(GET_STRING("log").c_str());

	struct fuse_args args = FUSE_ARGS_INIT(nagrc, nargv);

	ret = fuse_main(args.argc, args.argv, &sdfs_opt, NULL);
	fuse_opt_free_args(&args);
	return ret;
}